package com.ryan.order.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ryan.order.entity.PayLog;
import com.ryan.order.service.PayLogService;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-31
 */
@RestController
@RequestMapping("/order/payLog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    /**
     * 根据订单号生成微信支付二维码
     * @param orderNo
     * @return
     */
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo){
        Map<String, Object> map = payLogService.createNative(orderNo);
        System.out.println(map);
        return R.ok().data("map", map);
    }

    /**
     * 查询订单是否支付成功
     * @param orderNo
     * @return
     */
    @GetMapping("/queryOrderStatus/{orderNo}")
    public R queryOrderStatus(@PathVariable String orderNo){
        Map<String, String> map = payLogService.queryOrderStatus(orderNo);
        //如果为空，支付出错
        if (map == null)return R.error().message("支付出错");
        //如果不为空，要么支付中，要么支付成功
        if(map.get("trade_state").equals("SUCCESS")){
            //如果成功，则更新支付状态
            payLogService.updatePayStatus(map);
            System.out.println(map);
            return R.ok().message("支付成功");
        }
        //其他情况，则属于支付中
        return R.ok().code(25000).message("支付中，请稍后");
    }

    /**
     * 添加+分页查询所有的支付记录
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @return
     */
    @PostMapping("/getPayLogList/{pageIndex}/{pageSize}")
    public R getPayLogList(@PathVariable long pageIndex,
                          @PathVariable long pageSize,
                          @RequestBody(required = false) String orderNo
    ){
        Page<PayLog> resultPage = payLogService.getOrderList(pageIndex, pageSize, orderNo);
        return R.ok().data("list", resultPage.getRecords()).data("totalPage", resultPage.getTotal());
    }

    /**
     * 根据id获取支付记录
     * @param id
     * @return
     */
    @GetMapping("/getPayLogById/{id}")
    public R getOrderById(@PathVariable String id){
        PayLog payLog = payLogService.getById(id);
        return payLog==null?R.error().message("没有对应的订单支付记录"):R.ok().data("order", payLog);
    }

    /**
     * 增加支付记录
     * @param payLog
     * @return
     */
    @PostMapping("/addPayLog")
    public R addPayLog(@RequestBody PayLog payLog){
        boolean save = payLogService.save(payLog);
        return save?R.ok(): R.error();
    }

    /**
     * 更新支付记录
     * @param payLog
     * @return
     */
    @PostMapping("/updatePayLog")
    public R updatePayLog(@RequestBody PayLog payLog){
        boolean update = payLogService.updateById(payLog);
        return update?R.ok():R.error();
    }

    /**
     * 删除支付记录
     * @param ids
     * @return
     */
    @PostMapping("/deletePayLog")
    public R deletePayLog(@RequestBody Long[] ids){
        boolean delete = payLogService.removeByIds(Arrays.asList(ids));
        return delete?R.ok():R.error();
    }
}

